package io.sage.modules.integral.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.sage.modules.integral.dao.GIntegralDetailMapper;
import io.sage.modules.integral.entity.GIntegralDetail;
import io.sage.modules.integral.entity.GIntegralList;
import io.sage.modules.integral.enums.IntegralAddOrDelType;
import io.sage.modules.integral.service.GIntegralDetailService;
import io.sage.modules.integral.service.GIntegralListService;
import io.sage.modules.employer.entity.Employer;

@Service("gIntegralDetailService")
public class GIntegralDetailServiceImpl implements GIntegralDetailService {
	
	@Autowired
	private GIntegralDetailMapper integralDetailMapper;
	
	@Autowired
	private GIntegralListService integralListService;
	
	@Override
	public List<GIntegralDetail> selectIntegralDetailList(Map<String, Object> params) {
		
		return integralDetailMapper.selectIntegralDetailList(params);
	}

	@Override
	public int integralDetailOperation(Employer student, GIntegralDetail integralDetail) {
Integer operationType = integralDetail.getOperationType();
		
		GIntegralList integralList =new GIntegralList();
		integralList.setCreateTime(new Date());
		integralList.setDisFlag(0);
		integralList.setStudentId(student.getId());
		integralList.setUpdateTime(new Date());
		//如果是添加方法，就走进这个方法.
		//获取学生的积分详情
		GIntegralList selectByStudentId = integralListService.selectByStudentId(student.getId());
		if(IntegralAddOrDelType.ADD_INTEGRAL.getCode() == operationType){
			//如果学生没有记录，默认添加数据
			if(selectByStudentId == null){
				integralList.setIntegralSum(integralDetail.getIntegral());
				int saveIntegralList = integralListService.saveIntegralList(integralList);
				
				if(saveIntegralList<0){
					return -1;
				}
			}else {
				//学生当前的积分
				String integralSum = selectByStudentId.getIntegralSum();
				BigDecimal integralSumh = new BigDecimal(integralSum);
				//学生需要添加的积分
				String integral = integralDetail.getIntegral();
				BigDecimal integralh = new BigDecimal(integral);
				integralList.setIntegralSum(integralSumh.add(integralh).toString());
				int saveInt = integralListService.updateByStudentId(integralList);
				
				if(saveInt<0){
					return -1;
				}
			}
			
		//进行积分相减.
		}else {
			//如果学生没有积分,就进行扣除积分操作,则返回失败
			if(selectByStudentId == null){
				return -1;
			}
			
			//学生当前的积分
			String integralSum = selectByStudentId.getIntegralSum();
			
			if(Integer.parseInt(integralSum)<=0){
				
				return -1;
			}
			BigDecimal integralSumh = new BigDecimal(integralSum);
			//学生需要添加的积分
			String integral = integralDetail.getIntegral();
			BigDecimal integralh = new BigDecimal(integral);
			integralList.setIntegralSum(integralSumh.subtract(integralh).toString());
			int saveInt = integralListService.updateByStudentId(integralList);
			
			if(saveInt<0){
				return -1;
			}
			
		}
		integralDetail.setCreateTime(new Date());
		integralDetail.setUpdateTime(new Date());
		integralDetail.setStudentId(student.getId());
		integralDetail.setDisFlag(0);
		return integralDetailMapper.insertSelective(integralDetail);
	}

	@Override
	public int insertSelective(GIntegralDetail record) {
		// TODO Auto-generated method stub
		return integralDetailMapper.insertSelective(record);
	}

}
